<template>
	<view class="note">
		<view class="note-section">
			<u-checkbox-group shape="circle" @change="checkboxGroupChange">
				<div class="title">{{ $t('chat.friend')}}</div>
				<view class="note-item u-flex" v-for="(item, index) in lists" :key="index">
					<view class="note-item_avatar">
						<u-image v-if="item.FriendAvatar" width="72rpx" height="72rpx" border-radius="72rpx"
							:src="item.FriendAvatar"></u-image>
						<u-image v-else width="72rpx" height="72rpx" border-radius="72rpx"
							src="/static/images/avatar.jpg"></u-image>
					</view>
					<view class="note-item_content u-flex u-flex-1 u-border-bottom">
						<view class="note-item_content--name u-flex-1">{{item.FriendName}}</view>
						<u-checkbox :name="index" v-model="item.checked"></u-checkbox>
					</view>
				</view>
			</u-checkbox-group>
			<u-checkbox-group shape="circle" @change="checkboxGroupChange2">
				<div class="title">{{ $t('chat.chat_group') }}</div>
				<view class="note-item u-flex" v-for="(item, index) in groupList" :key="index">
					<view class="note-item_avatar">
						<u-image v-if="item.GroupAvatar" width="72rpx" height="72rpx" border-radius="72rpx"
							:src="item.GroupAvatar"></u-image>
						<u-image v-else width="72rpx" height="72rpx" border-radius="72rpx"
							src="/static/static/assets/gruopavatar.svg"></u-image>
					</view>
					<view class="note-item_content u-flex u-flex-1 u-border-bottom">
						<view class="note-item_content--name u-flex-1">{{item.GroupName}}</view>
						<u-checkbox :name="index" v-model="item.checked"></u-checkbox>
					</view>
				</view>
			</u-checkbox-group>
		</view>
		<u-loadmore :status="status" :icon-type="iconType" :load-text="loadText" margin-bottom="45" margin-top="45"
			color="#C1C0C0" />
	</view>
</template>

<script>
	import store from "@/pages/TUIKit/TUICore/store";
	import pageMixin from '@/utils/pageMixin';
	import {
		db
	} from "@/utils/sqlite";
	import sqliteSQL from "@/utils/sqliteSQL";
	import timStore from "../../TUIKit/TUICore/store/modules/timStore";
	export default {
		mixins: [pageMixin],
		data() {
			return {
				change1:'',
				lists: [],
				groupList: [],
				chooseIds: [],
				chooseIds2: [],
				status: 'nomore',
				iconType: 'flower',
				loadText: {
					loadmore: this.$t('chat.pull_up'),
					loading: this.$t('chat.loading'),
					nomore: this.$t('chat.note_no_more'),
				},
				friend: null,
				currentUser: null,
				content: '',
				messages: [],
				message: null,
				sendnum: 0,
				target:0,
				path12:'',
				merge:[],
				merge1:[],
				Imagemessge:{},
				filenew: {
					path: "",
					size: 0,
				},
				videoObj: {
					videoUrl: "",
					thumbnailUrl: ""
				},
				optinons: null,
			}
		},

		onLoad(option) {
			this.optinons = option;
			this.currentUser = this.vuex_user;
			this.getUserFriendList();
		},
		onShow() {
			uni.setNavigationBarTitle({
				title: this.$t('chat.select_friend')
			});
		},
		components: {},
		computed: {
			i18n() {
				return this.$t('chat')
			}
		},
		onNavigationBarButtonTap(e) {
			if (e.type == 'close') {
				this.$u.route({
					type: 'back',
					delta: 1
				});
				return;
			}

			//发送消息
			let that = this
			//检查是否勾选了好友群聊
			that.sendnum = that.chooseIds.length + that.chooseIds2.length
			if (that.sendnum == 0) {
				uni.showToast({
					title: this.$t('project.select_common_terms'),
					icon: 'none'
				})
				return;
			}
			that.chooseIds.forEach(function(index) {
				that.sendnum--
				//arr.push(that.lists[index])
				//that.friend = that.lists[index]
				//发送下信息
				that.change1='';
				if (that.optinons.type == 0) {
					let to = {
						to: that.lists[index].Friend.toString(),
						conversationType: 'C2C',
						avatar: that.$store.state.vuex_user.avatar,
						nickname: that.$store.state.vuex_user.uname,
						uid: that.$store.state.vuex_user.uid.toString(),
					};
						//单选转发图片，将存有地址的图片全局变量赋值给共用全局变量
						that.message = JSON.parse(that.optinons.messages);
						if(that.message.type==='TIMImageElem'){
							store.commit("timStore/setCommonpath",store.state.timStore.imagepath)
							that.forwardMessage(
								that.message,
								to
							);
						}
						//单选转发视频，将存有地址的视频全局变量赋值给共用全局变量
						if(that.message.type==='TIMVideoFileElem'){
							store.commit("timStore/setCommonpath",store.state.timStore.videopath)
							that.forwardMessage(
								that.message,
								to
							);
						}
						//单选转发文件，将存有地址的文件全局变量赋值给共用全局变量
						if(that.message.type==='TIMFileElem'){
							store.commit("timStore/setCommonpath",store.state.timStore.filepath)
							that.forwardMessage(
								that.message,
								to
							);
						}
						//单选转发合并信息，将存有地址的合并信息全局变量赋值给共用全局变量
						if(that.message.type==='TIMRelayElem'){
						var x=0;
						for( var i  in that.message.payload.messageList){
							let msg = {
								ID:that.message.payload.messageList[i].ID,
								conversationID:that.message.conversationID,
								conversationType:that.message.conversationType,
								type:that.message.payload.messageList[i].messageBody[0].type,
								payload:that.message.payload.messageList[i].messageBody[0].payload,
								nick:that.message.payload.messageList[i].nick,
								avatar:that.message.payload.messageList[i].avatar,
							}
							that.merge1[i]=msg
							console.log('ddddddddddddddddddddddddddddddddddddd',JSON.stringify(that.merge1))
						}

							let data = {
								to:to.to,
								conversationType:to.conversationType,
								messageList:that.merge1,
								title:'合并消息记录',
								abstractList:['allen: 666', 'iris: [图片]', 'linda: [文件]'],
								compatibleText:'请升级IDOCK到最新版本查看此消息'
							}
							that.sendMergerMessage(
								data
							);
						}

				} else if (that.optinons.type == 1) { //逐条转发
					let to = {
						to: that.lists[index].Friend.toString(),
						conversationType: 'C2C',
						avatar: that.$store.state.vuex_user.avatar,
						nickname: that.$store.state.vuex_user.uname,
						uid: that.$store.state.vuex_user.uid.toString(),
						};     
						var tap=0
						var tap1=0
						var tap2=0
						var tap3=0
						//循环遍历转发的消息数组
						for(var i in JSON.parse(that.optinons.messages)){
						that.message = JSON.parse(that.optinons.messages)[i];
						if(that.message.type==='TIMImageElem'){
							store.commit("timStore/setCommonpath",store.state.timStore.imagepath[tap])
							console.log('dadadadadadadada',JSON.stringify(store.state.timStore.commonpath))
							tap++
							that.forwardMessage(
								that.message,
								to
								);	
						}
							else if(that.message.type==='TIMTextElem'){
							that.forwardMessage(
								that.message,
								to
							);	
						}
							else if(that.message.type==='TIMSoundElem'){  							
							store.commit("timStore/setCommonpath",store.state.timStore.soundpath[tap3])
							tap3++
							that.forwardMessage(
								that.message,
								to
							);	
						}
							else if(that.message.type==='TIMVideoFileElem'){
							store.commit("timStore/setCommonpath",store.state.timStore.videopath[tap1])
							tap1++
							that.forwardMessage(
								that.message,
								to
							);	
						}
							else if(that.message.type==='TIMFileElem'){
								store.commit("timStore/setCommonpath",store.state.timStore.filepath[tap2])
								tap2++
								that.forwardMessage(
									that.message,
									to
								);	
							}
					}
				} else if (that.optinons.type == 2) { //合并转发(待处理)						
						that.merge = JSON.parse(that.optinons.messages)
						let to = {
							to: that.lists[index].Friend.toString(),
							conversationType: 'C2C',
							avatar: that.$store.state.vuex_user.avatar,
							nickname: that.$store.state.vuex_user.uname,
							uid: that.$store.state.vuex_user.uid.toString(),
							};    
						let data = {
							to:to.to,
							conversationType:to.conversationType,
							messageList:that.merge,
							title:'合并消息记录',
							abstractList:['allen: 666', 'iris: [图片]', 'linda: [文件]'],
							compatibleText:'请升级IDOCK到最新版本查看此消息'
						}
						that.sendMergerMessage(
							data
						);					
				}
			});
			that.chooseIds2.forEach(function(index) {
				that.sendnum--
				//arr.push(that.groupList[index])
				console.log(that.groupList[index]);
				//that.friend = that.lists[index]
				//单次转发类型type0
				if (that.optinons.type == 0) {
					let to = {
						to: that.groupList[index].Gid.toString(),
						conversationType: 'GROUP',
						avatar: that.$store.state.vuex_user.avatar,
						nickname: that.$store.state.vuex_user.uname,
						uid: that.$store.state.vuex_user.uid.toString(),
					};
					that.message = JSON.parse(that.optinons.messages);
					//单选转发图片，将存有地址的图片全局变量赋值给共用全局变量
					if(that.message.type==='TIMImageElem'){
						store.commit("timStore/setCommonpath",store.state.timStore.imagepath)
						that.forwardMessage(
							that.message,
							to
						);
					}
					//单选转发视频，将存有地址的图片全局变量赋值给共用全局变量
					if(that.message.type==='TIMVideoFileElem'){
						store.commit("timStore/setCommonpath",store.state.timStore.videopath)
						that.forwardMessage(
							that.message,
							to
						);
					}
					//单选转发文件，将存有地址的文件全局变量赋值给共用全局变量
					if(that.message.type==='TIMFileElem'){
						store.commit("timStore/setCommonpath",store.state.timStore.filepath)
						that.forwardMessage(
							that.message,
							to
						);
					}
					//单选转发合并信息，将存有地址的合并信息全局变量赋值给共用全局变量
					if(that.message.type==='TIMRelayElem'){
					var x=0;
					for( var i  in that.message.payload.messageList){
						let msg = {
							ID:that.message.payload.messageList[i].ID,
							conversationID:that.message.conversationID,
							conversationType:that.message.conversationType,
							type:that.message.payload.messageList[i].messageBody[0].type,
							payload:that.message.payload.messageList[i].messageBody[0].payload,
							nick:that.message.payload.messageList[i].nick,
							avatar:that.message.payload.messageList[i].avatar,
						}
						that.merge1[i]=msg
						console.log('ddddddddddddddddddddddddddddddddddddd',JSON.stringify(that.merge1))
					}
					
						let data = {
							to:to.to,
							conversationType:to.conversationType,
							messageList:that.merge1,
							title:'合并消息记录',
							abstractList:['allen: 666', 'iris: [图片]', 'linda: [文件]'],
							compatibleText:'请升级IDOCK到最新版本查看此消息'
						}
						that.sendMergerMessage(
							data
						);
					}
				} else if (that.optinons.type == 1) { //逐条转发
				//转发到群组
					let to = {
						to: that.groupList[index].Gid.toString(),
						conversationType: 'GROUP',
						avatar: that.$store.state.vuex_user.avatar,
						nickname: that.$store.state.vuex_user.uname,
						uid: that.$store.state.vuex_user.uid.toString(),
					};
						var tap=0
						var tap1=0
						var tap2=0
						var tap3=0
						//循环遍历转发的消息数组
						for(var i in JSON.parse(that.optinons.messages)){
						that.message = JSON.parse(that.optinons.messages)[i];
						    //如果有图片信息
							if(that.message.type==='TIMImageElem'){
							store.commit("timStore/setCommonpath",store.state.timStore.imagepath[tap])
							tap++
							that.forwardMessage(
								that.message,
								to
									);	
						}else if(that.message.type==='TIMTextElem'){ 				//如果有文字信息
							that.forwardMessage(
								that.message,
								to
							);	
						}else if(that.message.type==='TIMSoundElem'){ 
							store.commit("timStore/setCommonpath",store.state.timStore.soundpath[tap3])
							tap3++
							that.forwardMessage(
								that.message,
								to
							);	
						}else if(that.message.type==='TIMFileElem'){
								store.commit("timStore/setCommonpath",store.state.timStore.filepath[tap2])      //文件信息
								tap2++
								that.forwardMessage(
									that.message,
									to
								);	
						}else if(that.message.type==='TIMVideoFileElem'){
							store.commit("timStore/setCommonpath",store.state.timStore.videopath[tap1])				//如果有视频信息
							tap1++
							that.forwardMessage(
								that.message,
								to
							);	
						}
					}
				} else if (that.optinons.type == 2) { //合并转发(待处理)
					that.merge = JSON.parse(that.optinons.messages)
					let to = {
						to: that.groupList[index].Gid.toString(),
						conversationType: 'GROUP',
						avatar: that.$store.state.vuex_user.avatar,
						nickname: that.$store.state.vuex_user.uname,
						uid: that.$store.state.vuex_user.uid.toString(),
					};    
					let data = {
							to:to.to,
							conversationType:to.conversationType,
							messageList:that.merge,
							title:'合并消息记录',
							abstractList:['allen: 666', 'iris: [图片]', 'linda: [文件]'],
							compatibleText:'请升级IDOCK到最新版本查看此消息'
					}
					that.sendMergerMessage(
						data
					);		
				} else if (that.optinons.type == 3) { //合并转发(待处理)

				}
			});
			uni.showToast({
				title: this.$t('mine.send_success'),
			})
			setTimeout(() => {
				if (that.sendnum == 0) {
					that.$u.route({
						type: 'back',
						delta: 1
					});
					return;
				}
			}, 1000)
		},
		methods: {
			ToChangeMessages(messages) {
				messages.forEach(message => {
					console.log("接受批量转发信息" + JSON.stringify(message));
				})
			},
			async forwardMessage(message, to) {
				const TUIServer = uni.$TUIKit.TUIChatServer;
				await TUIServer.forwardMessage(message, to);
			},
			async sendMergerMessage(data) {
				const TUIServer = uni.$TUIKit.TUIChatServer;
				// console.log('是否进入send界面11111111111111111111111111111111111111'+JSON.stringify(data))
				await TUIServer.sendMergerMessage(data);

			},
			getUserFriendList() {
				this.$http.get('/mgr/user/friend/data', {}).then(res => {
					this.lists = res;
				});
				this.$http.get('/mgr/user/group/list/data', {}).then(res => {
					this.groupList = res;
				});
			},
			checkboxGroupChange(e) {
				this.chooseIds = e;
				console.log(this.chooseIds);
			},
			checkboxGroupChange2(e) {
				this.chooseIds2 = e;
				console.log(this.chooseIds2);
			},
			tabChange(index) {
				this.current = index;
			},
			onNavigationBarButtonTap(e) {
				console.log("触发了。。。")
				console.log("e", e)
				// this.sendnum = this.chooseIds.length + this.chooseIds2.length
				// //var arr = [];
				// var that = this;


				// if (e.type == 'close') {
				// 	that.$u.route({
				// 		type: 'back',
				// 		delta: 1
				// 	});
				// 	return;
				// }

				// that.chooseIds.forEach(function(index) {
				// 	that.sendnum--
				// 	//arr.push(that.lists[index])
				// 	console.log(that.lists[index]);
				// 	//that.friend = that.lists[index]
				// 	//发送下信息
				// 	if (that.optinons.type == 0) {
				// 		that.message = JSON.parse(that.optinons.messages);
				// 		that.createTextMessage(that.lists[index])
				// 	} else if (that.optinons.type == 1) { //逐条转发
				// 		// console.log("=====that.optinons.messages.length========"+that.optinons.messages.length)
				// 		var arr = JSON.parse(that.optinons.messages)
				// 		arr.forEach(function(message, indexs) {
				// 			//that.message =that.optinons.messages[indexs];
				// 			that.message = message;
				// 			// console.log("=====message========"+message)
				// 			that.createTextMessage(that.lists[index])
				// 		})
				// 	} else if (that.optinons.type == 2) { //合并转发(待处理)
				// 		//var messages={};
				// 		//messages.name =
				// 		var friend = that.lists[index]
				// 		var arr = JSON.parse(that.optinons.messages)
				// 		//arr.forEach(function(message,indexs){
				// 		//that.message =that.optinons.messages[indexs];
				// 		that.message = JSON.stringify(arr);
				// 		let imageMessage = that.goEasy.im.createCustomMessage({
				// 			type: 'mergeTransmit',
				// 			payload: that.message,
				// 			to: {
				// 				id: friend.Friend,
				// 				type: that.GoEasy.IM_SCENE.PRIVATE,
				// 				data: {
				// 					name: friend.FriendName,
				// 					avatar: friend.FriendAvatar != '' ? friend.FriendAvatar :
				// 						'../../../static/images/avatar.jpg'
				// 				}
				// 			},
				// 			onProgress: function(progress) {
				// 				console.log(progress)
				// 			},
				// 			notification: {
				// 				title: that.currentUser.uname + that.$t('chat.chat_send_text'),
				// 				body: '[' + that.$t('chat.chat_image_message') +
				// 					']' // 字段最长 50 字符
				// 			}
				// 		});
				// 		that.sendMessage(imageMessage);

				// 	}
				// });
				// that.chooseIds2.forEach(function(index) {
				// 	that.sendnum--
				// 	//arr.push(that.groupList[index])
				// 	console.log(that.groupList[index]);
				// 	//that.friend = that.lists[index]
				// 	//发送下信息
				// 	if (that.optinons.type == 0) {
				// 		that.message = JSON.parse(that.optinons.messages);
				// 		that.createTextMessage2(that.groupList[index])
				// 	} else if (that.optinons.type == 1) { //逐条转发
				// 		// console.log("=====that.optinons.messages.length========"+that.optinons.messages.length)
				// 		var arr = JSON.parse(that.optinons.messages)

				// 		arr.forEach(function(message, indexs) {
				// 			//that.message =that.optinons.messages[indexs];
				// 			that.message = message;
				// 			// console.log("=====message========"+message)
				// 			that.createTextMessage2(that.groupList[index])
				// 		})
				// 	} else if (that.optinons.type == 2) { //合并转发(待处理)
				// 		// console.log("=====合并信息往群里发送messages.length========"+that.optinons.messages.length)
				// 		//var messages={};
				// 		//messages.name =
				// 		var group = that.groupList[index]
				// 		var arr = JSON.parse(that.optinons.messages)
				// 		//arr.forEach(function(message,indexs){
				// 		//that.message =that.optinons.messages[indexs];
				// 		that.message = JSON.stringify(arr);
				// 		let imageMessage = that.goEasy.im.createCustomMessage({
				// 			type: 'mergeTransmit',
				// 			payload: that.message,
				// 			to: {
				// 				id: group.Gid,
				// 				type: that.GoEasy.IM_SCENE.GROUP,
				// 				data: {
				// 					name: group.GroupName,
				// 					avatar: group.GroupAvatarImg != '' ? group.GroupAvatarImg :
				// 						'/static/static/assets/gruopavatar.svg'
				// 				}
				// 			},
				// 			notification: {
				// 				title: that.currentUser.uname + that.$t('chat.chat_send_message'),
				// 				body: '[' + that.$t('chat.chat_image_message') + ']' // 字段最长 50 字符
				// 			}
				// 		});
				// 		that.sendMessage(imageMessage);
				// 	} else if (that.optinons.type == 3) { //合并转发(待处理)
				// 		// console.log("=====合并信息往群里发送messages.length========"+that.optinons.messages.length)
				// 		//var messages={};
				// 		//messages.name =
				// 		var group = that.groupList[index]
				// 		var arr = JSON.parse(that.optinons.messages)
				// 		// console.log("--一条合并转发数据payload----："+arr[0].payload)
				// 		var arrpayload = JSON.parse(arr[0].payload)
				// 		//arr.forEach(function(message,indexs){
				// 		//that.message =that.optinons.messages[indexs];
				// 		that.message = JSON.stringify(arrpayload);
				// 		let imageMessage = that.goEasy.im.createCustomMessage({
				// 			type: 'mergeTransmit',
				// 			payload: that.message,
				// 			to: {
				// 				id: group.Gid,
				// 				type: that.GoEasy.IM_SCENE.GROUP,
				// 				data: {
				// 					name: group.GroupName,
				// 					avatar: group.GroupAvatarImg != '' ? group.GroupAvatarImg :
				// 						'/static/static/assets/gruopavatar.svg'
				// 				}
				// 			},
				// 			notification: {
				// 				title: that.currentUser.uname + that.$t('chat.chat_send_message'),
				// 				body: '[' + that.$t('chat.chat_image_message') +
				// 					']' // 字段最长 50 字符
				// 			}
				// 		});
				// 		that.sendMessage(imageMessage);
				// 	}


				// });
				// uni.showToast({
				// 	title: this.$t('mine').send_success
				// })
				// setTimeout(() => {
				// 	if (that.sendnum == 0) {
				// 		that.$u.route({
				// 			type: 'back',
				// 			delta: 1
				// 		});
				// 		return;
				// 	}
				// }, 1000)
			},
			sendMessage(message) {
				// console.log("======sendMessage========");

				console.log("Messages", message)

				let that = this
				let messageId = message.messageId
				let senderData = message.senderData || {}
				let payload = message.payload || {}
				if (typeof payload === 'string') {
					payload = {}
				}
				let video = message.payload.video || {}
				let thumbnail = message.payload.thumbnail || {}
				let payloadUrl = message.payload.url || ''
				console.log("video", video)
				let videoUrl = video.url || ''
				console.log("videoUrl", videoUrl)
				let thumbnailUrl = thumbnail.url || ''
				let insertData = []

				let data = {
					'message_id': message.messageId,
					'receiver_id': message.receiverId || '',
					'sender_id': message.senderId,
					'sender_name': senderData.name || '',
					'sender_avatar': senderData.avatar || '',
					'timestamp': message.timestamp,
					'message_type': message.type,
					'message_recalled': message.recalled ? 1 : 0,
					'message_status': message.status,
					'message_read': message.read ? 1 : 0,
					'payload_text': message.payload.text || '',
					'payload_name': message.payload.name || '',
					'payload_content_type': message.payload.contentType || '',
					'payload_url': payloadUrl,
					'payload_duration': message.payload.duration || '',
					'payload_size': message.payload.size || '',
					'payload_width': message.payload.width || '',
					'payload_height': message.payload.height || '',
					'video_name': video.name || '',
					'video_content_type': video.contentType || '',
					'video_url': videoUrl,
					'video_duration': video.duration || '',
					'video_width': video.width || '',
					'video_height': video.height || '',
					'video_size': video.size || '',
					'thumbnail_content_type': thumbnail.contentType || '',
					'thumbnail_url': thumbnailUrl,
					'thumbnail_width': thumbnail.width || '',
					'thumbnail_height': thumbnail.height || '',
					'payload_video_url': message.payload.videoUrl || '',
					'payload_thumbnai_url': message.payload.thumbnailUrl || '',
					'payload_path': message.payload.path || '',
				};
				console.log("data", data)
				let keys = Object.keys(data)
				let keyStr = keys.toString()
				let valStr = ''
				for (let key in data) {
					valStr += ('"' + data[key] + '",')
				}
				valStr = valStr.substr(0, valStr.length - 1);
				let sqlStr = `INSERT INTO im_conversation_message(${keyStr}) values(${valStr})`
				insertData.push(sqlStr)

				if (message.type === 'mergeTransmit') {

					//补充合并消息
					payload = JSON.parse(message.payload)
					this.saveMergeMessage(message.receiverId, messageId, payload)
				}

				this.messages.push(message);

				//this.scrollToBottom();
				this.goEasy.im.sendMessage({
					message: message,
					onSuccess: function(message) {
						console.log('发送成功.', message);

						that.insertSQLiteMessageData(insertData)
						let thumbnail = message.payload.thumbnail || {}

						//下载图片、视频、音频到本地
						that.downloadMessageFile(payloadUrl, messageId, 'payload_url')
						that.downloadMessageFile(videoUrl, messageId, 'video_url')
						that.downloadMessageFile(message.payload.thumbnail.url || '', messageId,
							'thumbnail_url')
						that.downloadMessageFile(senderData.avatar || '', messageId, 'sender_avatar')

						if (this.sendnum == 0) {
							this.$u.route({
								type: 'back',
								delta: 1
							})
						}

						//返回处理
					},
					onFailed: function(error) {
						if (error.code === 507) {
							console.log(
								'发送语音/图片/视频/文件失败，没有配置OSS存储，详情参考：https://www.goeasy.io/cn/docs/goeasy-2.x/im/message/media/send-media-message.html'
							);
						} else {
							console.log('发送失败:', error);
						}
					}
				});
			},
			saveMergeMessage(groupId, mergeId, messages) {

				console.log("messages", messages)

				let insertData = []
				for (let i = 0; i < messages.length; i++) {

					delete messages[i].merge_data
					messages[i].merge_id = mergeId
					let keys = Object.keys(messages[i])
					let keyStr = keys.toString()
					let valStr = ''
					for (let key in messages[i]) {
						valStr += ('"' + messages[i][key] + '",')
					}
					valStr = valStr.substr(0, valStr.length - 1);
					let sqlStr = `INSERT INTO im_merge_message(${keyStr}) values(${valStr})`

					insertData.push(sqlStr)
				}
				console.log("messages", messages)

				this.insertSQLiteMessageData(insertData)
			},
			downloadMessageFile(url, messageId, key, table = 'im_conversation_message') {
				let that = this
				console.log("下载消息的图片", url)
				if (url != '' && url.indexOf('http') == 0) {
					uni.downloadFile({
						url: url,
						success: (res) => {
							if (res.statusCode === 200) {
								uni.saveFile({
									tempFilePath: res.tempFilePath,
									success: function(result) {

										console.log("下载好了", result.savedFilePath)
										//更新消息数据
										that.updateSQLiteMessageData(messageId, result
											.savedFilePath, key, table)
									},
								})
							}
						}
					})
				} else if (url != '') {
					uni.saveFile({
						tempFilePath: url,
						success: function(result) {

							console.log("前面的地址", url)
							console.log("下载厚的地址", result.savedFilePath)
							console.log("保存的地址", key)
							//更新消息数据
							that.updateSQLiteMessageData(messageId, result.savedFilePath, key, table)
						},
					})
				}
			},
			insertSQLiteMessageData(insertData) {

				let dbname = sqliteSQL.db.dbName
				let path = '_doc/' + this.vuex_user.uid + '.db'

				//打开数据库
				let isOpen = db.isOpenDB(dbname, path)
				if (isOpen) {
					plus.sqlite.executeSql({
						name: dbname,
						sql: insertData,
						success: (e) => {
							console.log("历史消息新增成功", e);
						},
						fail: (e) => {
							console.log("历史消息新增失败", e);
						}
					})
				}
			},
			updateSQLiteMessageData(messageId, url, key, table) {

				let dbname = sqliteSQL.db.dbName
				let path = '_doc/' + this.vuex_user.uid + '.db'
				let updateSql = `UPDATE ${table} SET ${key} = "${url}" WHERE message_id = "${messageId}";`


				console.log("updateSql", updateSql)

				//打开数据库
				let isOpen = db.isOpenDB(dbname, path)
				if (isOpen) {
					plus.sqlite.executeSql({
						name: dbname,
						sql: updateSql,
						success: (e) => {
							console.log("消息图片更新成功", e);
						},
						fail: (e) => {
							console.log("消息图片更新失败", e);
						}
					})
				}
			},
			createTextMessage(friend) {
				// console.log("-------createTextMessage-----"+this.content.trim());
				if (this.message.type == "text") {
					this.content = this.message.payload.text
					if (this.content.trim() !== '') {
						let body = this.content;
						if (this.content.length >= 50) {
							body = this.content.substring(0, 30) + '...';
						}
						let textMessage = this.goEasy.im.createTextMessage({
							text: this.content,
							to: {
								id: friend.Friend,
								type: this.GoEasy.IM_SCENE.PRIVATE,
								data: {
									name: friend.FriendName,
									avatar: friend.FriendAvatar != '' ? friend.FriendAvatar :
										'../../../static/images/avatar.jpg'
								}
							},
							notification: {
								title: this.currentUser.uname + this.$t('chat.chat_send_text'),
								body: body
							}
						});
						this.sendMessage(textMessage);
					}

				} else if (this.message.type == "image") {

					// console.log(this.message.payload.url+"++++++++filenew+++++++++"+this.message.payload.size)
					this.filenew.path = this.message.payload.url;
					this.filenew.size = this.message.payload.size;
					let imageMessage = this.goEasy.im.createCustomMessage({
						type: 'transmitImage',
						payload: this.filenew,
						to: {
							id: friend.Friend,
							type: this.GoEasy.IM_SCENE.PRIVATE,
							data: {
								name: friend.FriendName,
								avatar: friend.FriendAvatar != '' ? friend.FriendAvatar :
									'../../../static/images/avatar.jpg'
							}
						},
						onProgress: function(progress) {
							console.log(progress)
						},
						notification: {
							title: this.currentUser.uname + this.$t('chat.chat_send_text'),
							body: '[' + this.$t('chat.chat_image_message') +
								']' // 字段最长 50 字符
						}
					});
					this.sendMessage(imageMessage);
				} else if (this.message.type == "transmitvideo") {
					this.videoObj.videoUrl = this.message.payload.videoUrl;
					this.videoObj.thumbnailUrl = this.message.payload.thumbnailUrl
					let imageMessage = this.goEasy.im.createCustomMessage({
						type: 'transmitvideo',
						payload: this.videoObj,
						to: {
							id: friend.Friend,
							type: this.GoEasy.IM_SCENE.PRIVATE,
							data: {
								name: friend.FriendName,
								avatar: friend.FriendAvatar != '' ? friend.FriendAvatar :
									'../../../static/images/avatar.jpg'
							}
						},
						onProgress: function(progress) {
							console.log(progress)
						},
						notification: {
							title: this.currentUser.uname + this.$t('chat.chat_send_text'),
							body: '[' + this.$t('chat.chat_image_message') +
								']' // 字段最长 50 字符
						}
					});
					this.sendMessage(imageMessage);
				} else if (this.message.type == "video") {
					this.videoObj.videoUrl = this.message.payload.video.url;
					this.videoObj.thumbnailUrl = this.message.payload.thumbnail.url
					let imageMessage = this.goEasy.im.createCustomMessage({
						type: 'transmitvideo',
						payload: this.videoObj,
						to: {
							id: friend.Friend,
							type: this.GoEasy.IM_SCENE.PRIVATE,
							data: {
								name: friend.FriendName,
								avatar: friend.FriendAvatar != '' ? friend.FriendAvatar :
									'../../../static/images/avatar.jpg'
							}
						},
						onProgress: function(progress) {
							console.log(progress)
						},
						notification: {
							title: this.currentUser.uname + this.$t('chat.chat_send_text'),
							body: '[' + this.$t('chat.chat_image_message') +
								']' // 字段最长 50 字符
						}
					});
					this.sendMessage(imageMessage);
				} else if (this.message.type == "transmitImage") {

					// console.log(this.message.payload.path+"++++++++filenew+++++++++"+this.message.payload.size)
					this.filenew.path = this.message.payload.path;
					this.filenew.size = this.message.payload.size;
					let imageMessage = this.goEasy.im.createCustomMessage({
						type: 'transmitImage',
						payload: this.filenew,
						to: {
							id: friend.Friend,
							type: this.GoEasy.IM_SCENE.PRIVATE,
							data: {
								name: friend.FriendName,
								avatar: friend.FriendAvatar != '' ? friend.FriendAvatar :
									'../../../static/images/avatar.jpg'
							}
						},
						onProgress: function(progress) {
							console.log(progress)
						},
						notification: {
							title: this.currentUser.uname + this.$t('chat.chat_send_text'),
							body: '[' + this.$t('chat.chat_image_message') +
								']' // 字段最长 50 字符
						}
					});
					this.sendMessage(imageMessage);
				} else if (this.message.type == "mergeTransmit") {
					let imageMessage = this.goEasy.im.createCustomMessage({
						type: 'mergeTransmit',
						payload: this.message.payload,
						to: {
							id: friend.Friend,
							type: this.GoEasy.IM_SCENE.PRIVATE,
							data: {
								name: friend.FriendName,
								avatar: friend.FriendAvatar != '' ? friend.FriendAvatar :
									'../../../static/images/avatar.jpg'
							}
						},
						onProgress: function(progress) {
							console.log(progress)
						},
						notification: {
							title: this.currentUser.uname + this.$t('chat.chat_send_text'),
							body: '[' + this.$t('chat.chat_image_message') +
								']' // 字段最长 50 字符
						}
					});
					this.sendMessage(imageMessage);
				}
			},
			createTextMessage2(group) {
				if (this.message.type == "text") {
					this.content = this.message.payload.text
					if (this.content.trim() !== '') {
						let body = this.content;
						if (this.content.length >= 50) {
							body = this.content.substring(0, 30) + '...';
						}
						let textMessage = this.goEasy.im.createTextMessage({
							text: this.content,
							to: {
								id: group.Gid,
								type: this.GoEasy.IM_SCENE.GROUP,
								data: {
									name: group.GroupName,
									avatar: group.GroupAvatarImg != '' ? group.GroupAvatarImg :
										'/static/static/assets/gruopavatar.svg'
								}
							},
							notification: {
								title: this.currentUser.uname + this.$t('chat.chat_send_text'),
								body: body // 字段最长 50 字符
							}
						});
						this.sendMessage(textMessage);
					}
					//this.content = '';
				} else if (this.message.type == "image") {
					// console.log(this.message.payload.url+"++++++++filenew+++++++++"+this.message.payload.size)
					this.filenew.path = this.message.payload.url;
					this.filenew.size = this.message.payload.size;
					// console.log("++++++++filenew+++++++++"+this.filenew)
					let imageMessage = this.goEasy.im.createCustomMessage({
						type: 'transmitImage',
						payload: this.filenew,
						to: {
							id: group.Gid,
							type: this.GoEasy.IM_SCENE.GROUP,
							data: {
								name: group.GroupName,
								avatar: group.GroupAvatarImg != '' ? group.GroupAvatarImg :
									'/static/static/assets/gruopavatar.svg'
							}
						},
						onProgress: function(progress) {
							console.log(progress)
						},
						notification: {
							title: this.currentUser.uname + this.$t('chat.chat_send_message'),
							body: '[' + this.$t('chat.chat_image_message') +
								']' // 字段最长 50 字符
						}
					});
					this.sendMessage(imageMessage);
				} else if (this.message.type == "transmitImage") {
					// console.log(this.message.payload.path+"++++++++filenew+++++++++"+this.message.payload.size)
					this.filenew.path = this.message.payload.path;
					this.filenew.size = this.message.payload.size;
					// console.log("++++++++filenew+++++++++"+this.filenew)
					let imageMessage = this.goEasy.im.createCustomMessage({
						type: 'transmitImage',
						payload: this.filenew,
						to: {
							id: group.Gid,
							type: this.GoEasy.IM_SCENE.GROUP,
							data: {
								name: group.GroupName,
								avatar: group.GroupAvatarImg != '' ? group.GroupAvatarImg :
									'/static/static/assets/gruopavatar.svg'
							}
						},
						onProgress: function(progress) {
							console.log(progress)
						},
						notification: {
							title: this.currentUser.uname + this.$t('chat.chat_send_message'),
							body: '[' + this.$t('chat.chat_image_message') +
								']' // 字段最长 50 字符
						}
					});
					this.sendMessage(imageMessage);
				} else if (this.message.type == "video") {
					this.videoObj.videoUrl = this.message.payload.video.url;
					this.videoObj.thumbnailUrl = this.message.payload.thumbnail.url
					let imageMessage = this.goEasy.im.createCustomMessage({
						type: 'transmitvideo',
						payload: this.videoObj,
						to: {
							id: group.Gid,
							type: this.GoEasy.IM_SCENE.GROUP,
							data: {
								name: group.GroupName,
								avatar: group.GroupAvatarImg != '' ? group.GroupAvatarImg :
									'/static/static/assets/gruopavatar.svg'
							}
						},
						onProgress: function(progress) {
							console.log(progress)
						},
						notification: {
							title: this.currentUser.uname + this.$t('chat.chat_send_message'),
							body: '[' + this.$t('chat.chat_image_message') +
								']' // 字段最长 50 字符
						}
					});
					this.sendMessage(imageMessage);
				} else if (this.message.type == "transmitvideo") {
					this.videoObj.videoUrl = this.message.payload.videoUrl;
					this.videoObj.thumbnailUrl = this.message.payload.thumbnailUrl
					let imageMessage = this.goEasy.im.createCustomMessage({
						type: 'transmitvideo',
						payload: this.videoObj,
						to: {
							id: group.Gid,
							type: this.GoEasy.IM_SCENE.GROUP,
							data: {
								name: group.GroupName,
								avatar: group.GroupAvatarImg != '' ? group.GroupAvatarImg :
									'/static/static/assets/gruopavatar.svg'
							}
						},
						onProgress: function(progress) {
							console.log(progress)
						},
						notification: {
							title: this.currentUser.uname + this.$t('chat.chat_send_message'),
							body: '[' + this.$t('chat.chat_image_message') +
								']' // 字段最长 50 字符
						}
					});
					this.sendMessage(imageMessage);
				} else if (this.message.type == "mergeTransmit") {
					let imageMessage = this.goEasy.im.createCustomMessage({
						type: 'mergeTransmit',
						payload: this.message.payload,
						to: {
							id: group.Gid,
							type: this.GoEasy.IM_SCENE.GROUP,
							data: {
								name: group.GroupName,
								avatar: group.GroupAvatarImg != '' ? group.GroupAvatarImg :
									'/static/static/assets/gruopavatar.svg'
							}
						},
						onProgress: function(progress) {
							console.log(progress)
						},
						notification: {
							title: this.currentUser.uname + this.$t('chat.chat_send_message'),
							body: '[' + this.$t('chat.chat_image_message') +
								']' // 字段最长 50 字符
						}
					});
					this.sendMessage(imageMessage);
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
	.note {
		padding-bottom: 88rpx;
	}

	.note-section {
		margin-bottom: 24rpx;

		.title {
			padding: 20rpx;
		}
	}

	.note-item {
		width: 100%;
		background-color: #fff;
		height: 106rpx;
		padding: 0 24rpx 0 18rpx;

		&_avatar {
			position: relative;
			margin-right: 24rpx;
		}

		&_content {
			height: 100%;

			&--name {
				font-size: 32rpx;
			}

			&--text {
				font-size: 24rpx;
				color: #949C9E;
			}

			.u-checkbox__label {
				display: none;
			}
		}

		&_time {
			font-size: 24rpx;
			height: 76rpx;
			color: #949C9E;
		}
	}

	// .btns{
	// 	padding: 20rpx 40rpx 0;
	// 	.btn{
	// 		margin: 30rpx 60rpx;
	// 	}
	// }
</style>
